% students:
% Gilad Darmon ID=046248357
% Shiran Stan-Meleh ID=039067608
function [ resImage ] = BilinearInterpolation( imageA, srcIndex, dstIndex ,newImageB )
%this function computes all the missing grayscale colors values using
%Bilinear interpolations , we compute the delta between what we have and
%what we need.as showen in class for each value we compute its 4 nearby
%values. and we use the mean from all 4.

resImage = imageA;
indexXC = ceil( srcIndex(1,:) );
indexXF = floor( srcIndex(1,:) );
indexYC = ceil( srcIndex(2,:) );
indexYF = floor( srcIndex(2,:) );

%compute the Delta for both axies
deltaX = srcIndex(1,:) - indexXF;
deltaY = srcIndex(2,:) - indexYF;
sizeDeltaX = size(deltaX);

%like show in class and wikipedia.
SE = newImageB(sub2ind(size(newImageB), indexXC,indexYF));
SW = newImageB(sub2ind(size(newImageB), indexXF,indexYF));
NE = newImageB(sub2ind(size(newImageB), indexXC,indexYC));
NW = newImageB(sub2ind(size(newImageB), indexXF,indexYC));

southMat = SE.*deltaX + SW.*(ones(sizeDeltaX) - deltaX);
northMat = NE.*deltaX + NW.*(ones(sizeDeltaX) - deltaX);
%total mean of all four nearby values.
all = northMat.*deltaY + southMat.*(ones(sizeDeltaX) - deltaY);

mask = sub2ind(size(resImage),dstIndex(1,:), dstIndex(2,:));
resImage(mask) = all;
end

